/*
 * $Id: DatabaseTreeMouseListenerUnitTest.java 56 2010-01-23 14:49:04Z aaron.day
 * $
 */
package org.crazydays.gameplan.db.swing;


import java.awt.event.MouseEvent;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import org.junit.*;

import static org.easymock.EasyMock.*;
import static org.easymock.classextension.EasyMock.*;

import org.crazydays.gameplan.controller.Controller;

/**
 * DatabaseTreeMouseListenerUnitTest
 */
public class DatabaseTreeMouseListenerUnitTest
{
    /**
     * Test mouseClicked(MouseEvent).
     */
    @Test
    public void testMouseClicked_bad_location()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();
        listener.setController(controller);

        // expect
        expect(event.getComponent()).andReturn(tree);
        expect(event.getX()).andReturn(1).anyTimes();
        expect(event.getY()).andReturn(1).anyTimes();
        expect(tree.getRowForLocation(1, 1)).andReturn(-1);

        // replay
        replay(controller, tree, event);

        // test
        listener.mouseClicked(event);

        // verify
        verify(controller, tree, event);
    }

    /**
     * Test mouseClicked(MouseEvent).
     */
    @Test
    public void testMouseClicked_root()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);
        TreePath path = createMock(TreePath.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();
        listener.setController(controller);

        // expect
        expect(event.getComponent()).andReturn(tree);
        expect(event.getX()).andReturn(1).anyTimes();
        expect(event.getY()).andReturn(1).anyTimes();
        expect(tree.getRowForLocation(1, 1)).andReturn(1);
        expect(tree.getPathForLocation(1, 1)).andReturn(path);
        tree.setSelectionPath(path);
        expect(path.getLastPathComponent()).andReturn(
            new DefaultMutableTreeNode());

        // replay
        replay(controller, tree, event, path);

        // test
        listener.mouseClicked(event);

        // verify
        verify(controller, tree, event, path);
    }

    /**
     * Test mouseClicked(MouseEvent).
     */
    @Test
    public void testMouseClicked_map()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);
        TreePath path = createMock(TreePath.class);
        MapNode node = createMock(MapNode.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();
        listener.setController(controller);

        // expect
        expect(event.getComponent()).andReturn(tree);
        expect(event.getX()).andReturn(1).anyTimes();
        expect(event.getY()).andReturn(1).anyTimes();
        expect(tree.getRowForLocation(1, 1)).andReturn(1);
        expect(tree.getPathForLocation(1, 1)).andReturn(path);
        tree.setSelectionPath(path);
        expect(path.getLastPathComponent()).andReturn(node).anyTimes();
        controller.openGameMap(isA(String.class));

        // replay
        replay(controller, tree, event, path, node);

        // test
        listener.mouseClicked(event);

        // verify
        verify(controller, tree, event, path, node);
    }

    /**
     * Test mousePressed(MouseEvent).
     */
    @Test
    public void testMousePressed()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();
        listener.setController(controller);

        // replay
        replay(controller, tree, event);

        // test
        listener.mousePressed(event);

        // verify
        verify(controller, tree, event);
    }

    /**
     * Test mouseReleased(MouseEvent).
     */
    @Test
    public void testMouseReleased()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();
        listener.setController(controller);

        // replay
        replay(controller, tree, event);

        // test
        listener.mouseReleased(event);

        // verify
        verify(controller, tree, event);
    }

    /**
     * Test mouseEntered(MouseEvent).
     */
    @Test
    public void testMouseEntered()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();

        // replay
        replay(controller, tree, event);

        // test
        listener.mouseEntered(event);

        // verify
        verify(controller, tree, event);
    }

    /**
     * Test mouseExited(MouseEvent).
     */
    @Test
    public void testMouseExited()
    {
        // mocks
        Controller controller = createMock(Controller.class);
        JTree tree = createMock(JTree.class);
        MouseEvent event = createMock(MouseEvent.class);

        // setup
        DatabaseTreeMouseListener listener = new DatabaseTreeMouseListener();

        // replay
        replay(controller, tree, event);

        // test
        listener.mouseExited(event);

        // verify
        verify(controller, tree, event);
    }
}
